package com.util;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import java.io.*;
import java.util.Date;
import java.util.Map;

public class Upload extends ActionSupport {
    private static final int BUFFER_SIZE = 16 * 1024;
    private File fujian;
    private String fujianFileName;
    private String fujianContentType;


    public String upload() {
        String newFujianName = new Date().getTime() + fujianFileName.substring(fujianFileName.indexOf("."));
        String dstPath = ServletActionContext.getServletContext().getRealPath("upload") + "\\" + newFujianName;
        File dstFile = new File(dstPath);
        copy(this.getFujian(), dstFile);
        Map request = (Map) ServletActionContext.getContext().get("request");
        request.put("newFujianName", newFujianName);
        request.put("oldFujianName", fujianFileName);
        request.put("fujianPath", "/upload" + "/" + newFujianName);
        return ActionSupport.SUCCESS;
    }


    private static void copy(File src, File dst) {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
            out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
            byte[] buffer = new byte[BUFFER_SIZE];
            int len = 0;
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public File getFujian() {
        return fujian;
    }


    public void setFujian(File fujian) {
        this.fujian = fujian;
    }


    public String getFujianContentType() {
        return fujianContentType;
    }


    public void setFujianContentType(String fujianContentType) {
        this.fujianContentType = fujianContentType;
    }


    public String getFujianFileName() {
        return fujianFileName;
    }


    public void setFujianFileName(String fujianFileName) {
        this.fujianFileName = fujianFileName;
    }

}
